package mk.com.ldm.corepoint.implementation.client.presenter;

import java.util.LinkedList;

import org.gwt.mosaic.ui.client.MessageBox;

import mk.com.ldm.corepoint.implementation.client.CoreServiceAsync;
import mk.com.ldm.corepoint.implementation.shared.WorkFlowObject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class WorkFlowPresenter implements Presenter{
	public interface Display{
		FlexTable getTable();
		Widget asWidget();
	}
	
	private CoreServiceAsync rpcService;
	private HandlerManager eventBus;
	private Display display;
	private LinkedList<WorkFlowObject> workFlows;
	
	public WorkFlowPresenter(CoreServiceAsync rpcService,
			HandlerManager eventBus, Display display,
			LinkedList<WorkFlowObject> workFlows) {
		super();
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		this.workFlows = workFlows;
	}

	@Override
	public void go(HasWidgets container) {
		draw();
		container.add(display.asWidget());
	}
	
	public void go(FlexTable container){
		container.clear();
		draw();
		container.setWidget(0, 0, display.asWidget());
	}
	
	private void draw(){
		FlexTable table = display.getTable();
		for(final WorkFlowObject wfo:workFlows){
			int row = Integer.parseInt(wfo.getPoz().split(",")[0]);
			int col = Integer.parseInt(wfo.getPoz().split(",")[1]);
			table.getFlexCellFormatter().setColSpan(row, col, wfo.getCsp());
			table.getFlexCellFormatter().setRowSpan(row, col, wfo.getRsp());
			Image im = new Image("/images"+wfo.getPic());
			im.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(wfo.getUrl());
				}
			});
			im.setStyleName("wfImage");
			table.setWidget(row, col, im);
		}
		//table.setBorderWidth(1);
		table.setCellPadding(0);
		table.setCellSpacing(0);
		
	}

}
